// Establish a cache name
const cacheName = "MyFancyCacheName_v1";

self.addEventListener("fetch", (event) => {
  console.log(
    "service worker interceptor: ",
    event.request.mode,
    event.request.url
  );
  event.respondWith(
    caches.open(cacheName).then((cache) => {
      // Go to the network first
      return fetch(event.request.url)
        .then((fetchedResponse) => {
          cache.put(event.request, fetchedResponse.clone());

          return fetchedResponse;
        })
        .catch(() => {
          // If the network is unavailable, use cached
          return cache.match(event.request.url);
        });
    })
  );
});
